﻿<?php
    require($_SERVER['DOCUMENT_ROOT']."/bootstrap.php");
    // Nếu đã đăng nhập thì chuyển sang index
    if($session_email != '')
        header('Location: /');
    
    // tạo mã bảo vệ...
    function create_captcha() {
		$salt = "abchefghjkmnpqrstuvwxyz0123456789";
		srand((double)microtime()*1000000);
		$i = 0;
        $new_captcha='';
		while ($i <= 3) {
			$num = rand() % 33;
			$tmp = substr($salt, $num, 1);
			$new_captcha = $new_captcha . $tmp;
			$i++;
		}
		return $new_captcha;
	}
    
    if(!isset($_POST['button']))
    {
        $founduser=0;
        $captcha=create_captcha();
        $smarty->assign("captcha",$captcha);
        $smarty->assign("founduser", $founduser);
        $smarty->display('../templates/register/index.tpl');
        die();
    }
    if(isset($_POST['button']))
    {
        $email=$_POST['email'];
        $pass=$_POST['pass'];
        $telephone=$_POST['phone'];
		$cmnd=$_POST['cmnd'];
		$address=$_POST['address'];
		$fullname=$_POST['fullname'];
    }
    if($email & $pass & $telephone)
    {
        $founduser=0; 
        $sql="select * from user where email='".$email."'";
        $query=mysql_query($sql);
        if(mysql_num_rows($query) != "" )
        {
            $founduser=2;
        }
        else
        {	    
            $sql2="insert into user(email,password,fullname,phone,address,idpermission,cmnd) values('".$email."','".md5($pass)."','".($fullname)."','".$telephone."','".($address)."','4',$cmnd)";
            
			$query2=mysql_query($sql2);
			
            //echo "Da them thanh vien moi thanh cong";
            $founduser=1; 
            $_SESSION['email']=$email;
        }
        $smarty->assign("founduser", $founduser);
    }
        $smarty->display('../templates/register/index.tpl');
?>